package com.rec.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.rec.entity.ConsumeLogs;
import com.rec.repository.ConsumeLogsRepository;
import com.rec.repository.StudentRepository;
import com.rec.repository.TeachersRepository;
import com.rec.service.ConsumeLogsService;
@Service
@Transactional
public class ConsumeLogsServiceImpl implements ConsumeLogsService {
		@Autowired
	    private ConsumeLogsRepository r;
		@Autowired
		private StudentRepository stur;
		@Autowired
		private TeachersRepository tear;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		/**
		 * 刷入刷出一体操作
		 */
		@Override
		public Integer getDo(String cardno,Integer id) {
			Integer num = null;
			if(stur.findByCardno(cardno)!=null&&stur.findByCardno(cardno).getStatus()==0) {
				//学生，状态为零，刷入
				num = 1;
				ConsumeLogs con = new ConsumeLogs();
				con.setReadroomid(id);;
				con.setCardno(cardno);
				con.setIntime(df.format(new Date()));// new Date()为获取当前系统时间
				con.setReadroomid(id);
				//保存记录
				r.save(con);
				//修改当前状态
				stur.updateStatus(cardno,id);
			}else if(tear.findByCardno(cardno)!=null&&tear.findByCardno(cardno).getStatus()==0) {
				//教师；状态为零 刷入
				num = 2;
				ConsumeLogs con = new ConsumeLogs();
				con.setReadroomid(id);
				//当前记录的图书馆id
				con.setCardno(cardno);
				con.setIntime(df.format(new Date()));// new Date()为获取当前系统时间
				con.setReadroomid(id);
				//保存记录
				r.save(con);
				//修改当前状态
				tear.updateStatus(cardno,id);
			}else if(stur.findByCardno(cardno)!=null&&stur.findByCardno(cardno).getStatus()!=0){
			    List<String> TimeList = r.getLastTime(cardno);
				//学生，状态不为零，执行刷出
				r.getOutSet(cardno,df.format(new Date()),TimeList.get(TimeList.size()-1));
				//修改当前状态为零
				stur.updateStatus(cardno,0);
				num = 3;
			}else if(tear.findByCardno(cardno)!=null&&tear.findByCardno(cardno).getStatus()!=0) {
				//教师状态不为零，执行刷出
				List<String> TimeList = r.getLastTime(cardno);
				r.getOutSet(cardno,df.format(new Date()),TimeList.get(TimeList.size()-1));
				//修改当前状态为零
				tear.updateStatus(cardno,0);
				num=4;
			}else {
				//卡号不存在
				num=5;
			}
			return num;
		}
		
		
		@Override
		public Map<String, Object> all(Map<String, Object> params) {
			Specification<ConsumeLogs> spec = getSpec(params);
			// 创建分页对象
			Integer page = (Integer) params.get("page");
			Integer limit = (Integer) params.get("limit");
			Pageable pa = PageRequest.of(page, limit);
			Page<ConsumeLogs> result = r.findAll(spec,pa);
			List<ConsumeLogs> data = result.getContent();
			long count = result.getTotalElements();
			if(data.size()>0) {
				params.put("code", 0);
				params.put("data", data);
				params.put("count", count);
				
			} else {
				params.put("code", 1);
				params.put("msg", "没有数据！");
			}
			return params;
		}
		private Specification<ConsumeLogs> getSpec(Map<String, Object> params) {
			return new Specification<ConsumeLogs>() {
				@Override
				public Predicate toPredicate(Root<ConsumeLogs> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
					Predicate pre = cb.conjunction();
					List<Expression<Boolean>> sqlList = pre.getExpressions();
					if(!StringUtils.isEmpty(params.get("cardno"))) {
						sqlList.add(cb.like(root.get("cardno"),params.get("cardno")+"%"));
					}
					if(!StringUtils.isEmpty(params.get("start"))) {
						sqlList.add(cb.greaterThan(root.get("intime"),(String)params.get("start")));
					}
					if(!StringUtils.isEmpty(params.get("end"))) {
						sqlList.add(cb.lessThan(root.get("outtime"),(String)params.get("end")));
					}
					return pre;
				}
				
			};
		}
		
		 
}
